from fastapi import HTTPException, APIRouter
from apps.schemas.schemas import ProfileUpdateRequest
from apps.services.user_profile import (
    get_user_profile,
    update_profile
)

user_profiles = APIRouter()


@user_profiles.get("/msg/{user_id}")
async def get_user_profile_api(user_id: int):
    """
    根据user_id获取用户的详细信息
    """
    try:
        user_profile_data = await get_user_profile(user_id)
        return user_profile_data
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@user_profiles.put("/update/{user_id}")
async def update_profile_api(user_id: int, data: ProfileUpdateRequest):
    """
    根据user_id更新用户信息（不区分学生还是教练）
    """
    try:
        updated = await update_profile(user_id, data)
        if updated:
            return {
                "status": 200,
                "message": "Profile updated successfully"
            }
    except ValueError as e:
        if "ID number already exists" in str(e):
            return {
                "status": 400,
                "message": str(e)
            }
        elif "User not found" in str(e) or "User profile not found" in str(e):
            return {
                "status": 404,
                "message": str(e)
            }
        else:
            return {
                "status": 404,
                "message": str(e)
            }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error updating profile: {str(e)}"
        }
